Page({
  data: {
    equipmentList: [],
    loading: true,
    adminInfo: null
  },
  
  onLoad() {
    this.checkAdminLogin();
  },
  
  onShow() {
    if (this.data.adminInfo) {
      this.loadEquipmentList();
    }
  },
  
  // 检查管理员登录
  async checkAdminLogin() {
    const adminInfo = wx.getStorageSync('adminInfo');
    const adminToken = wx.getStorageSync('adminToken');
    
    if (!adminInfo || !adminToken) {
      this.redirectToLogin();
      return;
    }
    
    try {
      // 检查管理员身份是否有效
      const result = await wx.cloud.callFunction({
        name: 'checkAdminAuth',
        data: {
          adminId: adminInfo.adminId,
          token: adminToken
        }
      });
      
      const { code, data, message } = result.result;
      
      if (code === 0 && data) {
        // 验证通过，设置管理员信息
        this.setData({
          adminInfo: {
            ...adminInfo,
            ...data
          }
        });
        
        // 加载装备列表
        this.loadEquipmentList();
      } else {
        // 验证失败，清除存储的信息并重定向到登录页
        wx.removeStorageSync('adminInfo');
        wx.removeStorageSync('adminToken');
        
        wx.showToast({
          title: message || '登录已过期，请重新登录',
          icon: 'none'
        });
        
        this.redirectToLogin();
      }
    } catch (error) {
      console.error('验证管理员身份错误', error);
      wx.showToast({
        title: '网络错误，请重试',
        icon: 'none'
      });
      this.redirectToLogin();
    }
  },
  
  // 加载装备列表
  async loadEquipmentList() {
    this.setData({ loading: true });
    
    try {
      const adminToken = wx.getStorageSync('adminToken');
      const result = await wx.cloud.callFunction({
        name: 'adminGetEquipments',
        data: {
          adminId: this.data.adminInfo.adminId,
          token: adminToken
        }
      });
      
      const { code, data, message } = result.result;
      
      if (code === 0 && data) {
        this.setData({
          equipmentList: data
        });
      } else {
        wx.showToast({
          title: message || '获取装备列表失败',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('获取装备列表失败', error);
      wx.showToast({
        title: '获取装备列表失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },
  
  // 添加新装备
  addEquipment() {
    wx.navigateTo({
      url: '/pages/admin/equipment/edit/edit'
    });
  },
  
  // 编辑装备
  editEquipment(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/admin/equipment/edit/edit?id=${id}`
    });
  },
  
  // 删除装备
  async deleteEquipment(e) {
    const { id, name } = e.currentTarget.dataset;
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除装备"${name}"吗？`,
      success: async (res) => {
        if (res.confirm) {
          try {
            const adminToken = wx.getStorageSync('adminToken');
            const result = await wx.cloud.callFunction({
              name: 'adminDeleteEquipment',
              data: {
                equipmentId: id,
                adminId: this.data.adminInfo.adminId,
                token: adminToken
              }
            });
            
            const { code, message } = result.result;
            
            if (code === 0) {
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              });
              
              // 重新加载装备列表
              this.loadEquipmentList();
            } else {
              wx.showToast({
                title: message || '删除失败',
                icon: 'none'
              });
            }
          } catch (error) {
            console.error('删除装备失败', error);
            wx.showToast({
              title: '删除失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },
  
  // 重定向到登录页
  redirectToLogin() {
    wx.redirectTo({
      url: '/pages/admin/login/login',
    });
  },
  
  // 返回管理后台首页
  navigateBack() {
    wx.navigateBack();
  }
}) 